home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Visual Basic new SourceCode and Projects / Barcode Generator 2.0 / Module1.bas < prev    next >
Encoding:
BASIC Source File  |  2000-06-21  |  9.8 KB  |  354 lines

  1. Attribute VB_Name = "Module1"
  2. Public bcode() As New BarCodefrm
  3.  
  4. Type FormState
  5.     Deleted As Integer
  6.     Dirty As Integer
  7.     Color As Long
  8. End Type
  9.  
  10. Public FState()  As FormState
  11.  
  12.  
  13. Sub newBarCode()
  14.     Dim fIndex As Integer
  15.  
  16.     fIndex = FindFreeIndex()
  17.     bcode(fIndex).Tag = fIndex
  18.     bcode(fIndex).Caption = "BCode -" & fIndex & "-"
  19.     bcode(fIndex).Show
  20. End Sub
  21. Function FindFreeIndex() As Integer
  22.     Dim i As Integer
  23.     Dim ArrayCount As Integer
  24.  
  25.     ArrayCount = UBound(bcode)
  26.  
  27.     ' Durchlaufen des Dokument-Datenfelds. Falls eines der Dokumente entfernt
  28.     ' wurde, wird dieser Index zurⁿckgeliefert.
  29.     For i = 1 To ArrayCount
  30.         If FState(i).Deleted Then
  31.             FindFreeIndex = i
  32.             FState(i).Deleted = False
  33.             Exit Function
  34.         End If
  35.     Next
  36.  
  37.     ' Falls keines der Dokumente entfernt wurde,
  38.     ' werden das Dokument- sowie das Status-Datenfeld erweitert und
  39.     ' der Index des neuen Elements zurⁿckgeliefert.
  40.     ReDim Preserve bcode(ArrayCount + 1)
  41.     ReDim Preserve FState(ArrayCount + 1)
  42.     FindFreeIndex = UBound(bcode)
  43. End Function
  44. Function CodeAToByte(Number As String) As String
  45. 'Decodes the number to a Binary A-Code
  46. 'A 1 is a Black Line
  47. 'A 0 is a White Line
  48. Select Case Number
  49. Case 0
  50.     CodeAToByte = "0001101"
  51. Case 1
  52.     CodeAToByte = "0011001"
  53. Case 2
  54.     CodeAToByte = "0010011"
  55. Case 3
  56.     CodeAToByte = "0111101"
  57. Case 4
  58.     CodeAToByte = "0100011"
  59. Case 5
  60.     CodeAToByte = "0110001"
  61. Case 6
  62.     CodeAToByte = "0101111"
  63. Case 7
  64.     CodeAToByte = "0111011"
  65. Case 8
  66.     CodeAToByte = "0110111"
  67. Case 9
  68.     CodeAToByte = "0001011"
  69. End Select
  70. End Function
  71. Function CodeBToByte(Number As String) As String
  72. 'Decodes the number to a Binary B-Code
  73. 'A 1 is a Black Line
  74. 'A 0 is a White Line
  75. Select Case Number
  76. Case 0
  77.     CodeBToByte = "0100111"
  78. Case 1
  79.     CodeBToByte = "0110011"
  80. Case 2
  81.     CodeBToByte = "0011011"
  82. Case 3
  83.     CodeBToByte = "0100001"
  84. Case 4
  85.     CodeBToByte = "0011101"
  86. Case 5
  87.     CodeBToByte = "0111001"
  88. Case 6
  89.     CodeBToByte = "0000101"
  90. Case 7
  91.     CodeBToByte = "0010001"
  92. Case 8
  93.     CodeBToByte = "0001001"
  94. Case 9
  95.     CodeBToByte = "0010111"
  96. End Select
  97. End Function
  98. Function CodeCToByte(Number As String) As String
  99. 'Decodes the number to a Binary C-Code
  100. 'A 1 is a Black Line
  101. 'A 0 is a White Line
  102. Select Case Number
  103. Case 0
  104.     CodeCToByte = "1110010"
  105. Case 1
  106.     CodeCToByte = "1100110"
  107. Case 2
  108.     CodeCToByte = "1101100"
  109. Case 3
  110.     CodeCToByte = "1000010"
  111. Case 4
  112.     CodeCToByte = "1011100"
  113. Case 5
  114.     CodeCToByte = "1001110"
  115. Case 6
  116.     CodeCToByte = "1010000"
  117. Case 7
  118.     CodeCToByte = "1000100"
  119. Case 8
  120.     CodeCToByte = "1001000"
  121. Case 9
  122.     CodeCToByte = "1110100"
  123. End Select
  124. End Function
  125. Function code(ByVal Number As String) As String
  126. 'Generates a sequence for the decoding of the next 6 numbers
  127. Select Case Number
  128. Case 0
  129.     code = "AAAAAA"
  130. Case 1
  131.     code = "AABBAB"
  132. Case 2
  133.     code = "AABBAB"
  134. Case 3
  135.     code = "AABBBA"
  136. Case 4
  137.     code = "ABAABB"
  138. Case 5
  139.     code = "ABBAAB"
  140. Case 6
  141.     code = "ABBBBA"
  142. Case 7
  143.     code = "ABABAB"
  144. Case 8
  145.     code = "ABABBA"
  146. Case 9
  147.     code = "ABBABA"
  148. End Select
  149. End Function
  150. Function PaintCode(frm As Form, fi, se, th)
  151. Dim reihe
  152. Dim z
  153. Dim B
  154. Dim D
  155. frm.Line (1 + 10, 0)-(1 + 10, 25) 'Paint the First two lines on the begin of the Code
  156. frm.Line (3 + 10, 0)-(3 + 10, 25)
  157. reihe = code(fi)
  158. For z = 1 To 6 'Use A and B code to Decode the Barcode 'For each 6 numbers use 7 Lines 6 * 7 = 47 Lines
  159.     If Mid(reihe, z, 1) = "A" Then 'Code A
  160.         B = CodeAToByte(Mid(se, z, 1))
  161.         For D = 1 To 7 'Paint the 7 Lines (A Code)
  162.             If Mid(B, D, 1) = 1 Then 'On all 7 numbers Check if it is a 1 or a 0 and Paint a Black or a White Line
  163.                 frm.Line ((z - 1) * 7 + D + 3 + 10, 0)-((z - 1) * 7 + D + 3 + 10, 20), &H0 'Black Line
  164.             Else
  165.                 frm.Line ((z - 1) * 7 + D + 3 + 10, 0)-((z - 1) * 7 + D + 3 + 10, 20), &HFFFFFF 'White Line
  166.             End If
  167.         Next
  168.     ElseIf Mid(reihe, z, 1) = "B" Then 'Code B
  169.         B = CodeBToByte(Mid(se, z, 1))
  170.         For D = 1 To 7 'Paint the 7 Lines (B Code)
  171.             If Mid(B, D, 1) = 1 Then 'On all 7 numbers Check if it is a 1 or a 0 and Paint a Black or a White Line
  172.                 frm.Line ((z - 1) * 7 + D + 3 + 10, 0)-((z - 1) * 7 + D + 3 + 10, 20), &H0 'Black Line
  173.             Else
  174.                 frm.Line ((z - 1) * 7 + D + 3 + 10, 0)-((z - 1) * 7 + D + 3 + 10, 20), &HFFFFFF 'White Line
  175.             End If
  176.         Next
  177.     End If
  178. Next
  179. frm.Line (6 * 7 + 5 + 10, 0)-(6 * 7 + 5 + 10, 25) 'Paint the middle two lines of the Code
  180. frm.Line (6 * 7 + 7 + 10, 0)-(6 * 7 + 7 + 10, 25)
  181.     For z = 1 To 6 'Use C code to Decode the Barcode 'For each 6 numbers use 7 Lines 6 * 7 = 47 Lines
  182.         B = CodeCToByte(Mid(th, z, 1)) ' Code C
  183.         For D = 1 To 7 'Paint the 7 Lines (C Code)
  184.             If Mid(B, D, 1) = 1 Then 'On all 7 numbers Check if it is a 1 or a 0 and Paint a Black or a White Line
  185.                 frm.Line ((z - 1) * 7 + D + 50 + 10, 0)-((z - 1) * 7 + D + 50 + 10, 20), &H0 'Black Line
  186.             Else
  187.                 frm.Line ((z - 1) * 7 + D + 50 + 10, 0)-((z - 1) * 7 + D + 50 + 10, 20), &HFFFFFF 'White Line
  188.             End If
  189.         Next
  190.     Next
  191. frm.Line (94 + 9, 0)-(94 + 9, 25) 'The Last two lines
  192. frm.Line (96 + 9, 0)-(96 + 9, 25)
  193. End Function
  194. Function CheckCode(FullCode As String) As Boolean 'Test the Code
  195. Dim A
  196. Dim B
  197. Dim C
  198. B = 1
  199. If Len(FullCode) = 13 Then
  200. For A = 1 To 12
  201.     If B = 1 Then
  202.         C = C + Mid(FullCode, A, 1)
  203.         B = 0
  204.     Else
  205.         C = C + (Mid(FullCode, A, 1) * 3)
  206.         B = 1
  207.     End If
  208. Next
  209. If (C + Mid(FullCode, 13, 1)) Mod 10 = 0 Then
  210.     CheckCode = True
  211. Else
  212.     CheckCode = False
  213. End If
  214. Else
  215.     CheckCode = False
  216. End If
  217. 'e.g:
  218. 'Code:   4  0  1  2  3  4  5  0  6  7  8  9  7
  219. '        *1|*3|*1|*3|*1|*3|*1|*3|*1|*3|*1|*3|*1
  220. 'Result: 4+ 0+ 1+ 6+ 3+ 12+5+ 0+ 6+ 21+8+ 27 +7 = 100  || 100 Mod 10 = 0 Code is Correct
  221. End Function
  222.  
  223.  
  224. 'Bar Code
  225. '           ||||||||||||||||||
  226. '           ||||||||||||||||||
  227. '           ||||||||||||||||||
  228. '          4||012345||067897||
  229. '         1.    2.      3.
  230. '1. First Number: Is used to get how 2. is Decoded
  231. '2. 6 Numbers:Are Decoded in A and B Code
  232. '3. Last 6 Numbers: Are always Decoded in C Code
  233. 'The First 2 Numbers are the Country code.
  234. 'The Next 5 the Manufacteur
  235. 'The Next 5 the Product
  236. 'And the Last is a Check Number to Check the Code
  237. 'e.g:   7610800002482
  238. '       76    = Switzerland
  239. '       10800 = Inter-Milk
  240. '       00248 = Pastmilk
  241. '       2     = Checknumber
  242.  
  243. '  Number 6 in code A
  244. '  | | |   |
  245. ' 00110011111111
  246. ' 00110011111111
  247. ' 00110011111111
  248. ' 00110011111111
  249. ' 00110011111111
  250. ' 00110011111111
  251. ' ^ ^ ^ ^ ^ ^ ^
  252. ' 0 1 0 1 1 1 1
  253. 'In the created Code for a Number must be 2 Big Black and 2 Big White lines(Not the small painted lines)
  254. 'e.g: 2(or more) small black(or White Lines) next to each other = 1 Big Line
  255. 'e.g: 1 small line = 1 Big Line
  256. Public Sub Code3of9(sToCode As String, pPaintInto As Form, pLabelInto As Label)
  257.     
  258.     Dim sValidChars As String
  259.     Dim sValidCodes As String
  260.     Dim lElevate As Integer
  261.     Dim lCounter As Long
  262.     Dim lWkValue As Long
  263.     Dim PosX As Long
  264.     Dim PosY1 As Long
  265.     Dim PosY2 As Long
  266.     Dim TPX As Long
  267.     
  268.     pPaintInto.Cls
  269.     
  270.     TPX = Screen.TwipsPerPixelX
  271.     
  272.     sValidChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"
  273.     sValidCodes = "41914595664727860970419025962647338417105957" + _
  274.     "84729059950476626106644590602984801043246599" + _
  275.     "62476744460260046477586109044686603224803443" + _
  276.     "91860130478424477058030365265828235758580903" + _
  277.     "65863556658042365383495434978353624150635770"
  278.     
  279.     sToCode = UCase(IIf(Left(sToCode, 1) = "*", "", "*") + sToCode + IIf(Right(sToCode, 1) = "*", "", "*"))
  280.     PosX = ((((pPaintInto.Width / TPX) - (Len(sToCode) * 16)) / 2) * TPX) - 1
  281.     PosY1 = pPaintInto.Height * 0.2
  282.     PosY2 = pPaintInto.Height * 0.8
  283.     
  284.  
  285.  
  286.     If PosX < 0 Then
  287.         MsgBox "The length of the code exceeds control limits.", vbExclamation, "Large string"
  288.         Mainfrm.ActiveForm.Width = InputBox("Set a new width", "New Width", Mainfrm.ActiveForm.Width)
  289.         GoTo end_Code
  290.     End If
  291.     
  292.     On Error Resume Next
  293.     
  294.  
  295.  
  296.     For lCounter = 1 To Len(sToCode)
  297.         'Here is where the number is fetched from the sValidCodes string. It will get only 5 digits.
  298.         lWkValue = Val(Mid(sValidCodes, ((InStr(1, sValidChars, Mid(sToCode, lCounter, 1)) - 1) * 5) + 1, 5))
  299.         lWkValue = IIf(lWkValue = 0, 36538, lWkValue)
  300.  
  301.  
  302.         For lElevate = 15 To 0 Step -1
  303.             'It evaluates the binary number to see if it has to draw a line.
  304.  
  305.  
  306.             If lWkValue >= 2 ^ lElevate Then
  307.                 pPaintInto.Line (PosX, 0)-(PosX, PosY2)
  308.                 lWkValue = lWkValue - (2 ^ lElevate)
  309.             End If
  310.             PosX = PosX + TPX
  311.         Next
  312.     Next
  313.     pLabelInto.Caption = Mid(sToCode, 2, Len(sToCode) - 2)
  314. end_Code:
  315. End Sub
  316. Function repair(code As String) As String
  317. If Len(code) >= 12 And IsNumeric(code) = True Then
  318. code = Mid$(code, 1, 12)
  319. code = code & "0"
  320. Do Until CheckCode(code) = True
  321. code = Mid$(code, 1, 12) & (Mid$(code, 13, 1) + 1)
  322. Loop
  323. repair = code
  324. Else
  325.     repair = 0
  326. End If
  327. End Function
  328. Function Add(code As String) As String
  329. If Len(code) >= 12 And IsNumeric(code) = True Then
  330. code = Mid$(code, 1, 12)
  331. code = code + 1
  332. code = code & "0"
  333. Do Until CheckCode(code) = True
  334. code = Mid$(code, 1, 12) & (Mid$(code, 13, 1) + 1)
  335. Loop
  336. Add = code
  337. Else
  338. Add = 0
  339. End If
  340. End Function
  341. Function Subt(code As String) As String
  342. If Len(code) >= 12 And IsNumeric(code) = True Then
  343. code = Mid$(code, 1, 12)
  344. code = code - 1
  345. code = code & "0"
  346. Do Until CheckCode(code) = True
  347. code = Mid$(code, 1, 12) & (Mid$(code, 13, 1) + 1)
  348. Loop
  349. Subt = code
  350. Else
  351. Subt = 0
  352. End If
  353. End Function
  354.